<?php

//	Author: 	Aris Michail
//	Email:		std03055@di.uoa.gr
//	Purpose: 	Get the values of the attributes of an entity

// REVISE attributes table on entity

require_once ("../DB/DB.php");
require_once ("../Frameworks/Objects/user.php");
require_once ("../Frameworks/Objects/category.php");

function DB_category_getentityvalues($categoryowner, $category, &$entity)
{
    global $conn;

    // The name of the stored procedure
    $sp_name = "usp_category_getentitieswithvalues";

    // The return variable
    $retval;

    // Get the values
    $owner = $categoryowner->GetUsername();
    $catname = $category->GetName();
    $entityname = $entity->GetName();

    // Create a new statement
    $stmt = mssql_init($sp_name, $conn);

    // Bind values
    mssql_bind($stmt, "RETVAL", &$retval, SQLINT4, true, false, 1);
    mssql_bind($stmt, '@ownername', &$owner, SQLVARCHAR, false, false, 50);
    mssql_bind($stmt, '@categoryname', &$catname, SQLVARCHAR, false, false, 50);
    mssql_bind($stmt, '@entityname', &$entityname, SQLVARCHAR, false, false, 50);

    // Execute the statement
    $status = mssql_execute($stmt);
    if (!$status)
        return DB_ERROR_CANNOTGETVALUESFORENTITY;
    else
    {
        $attributes = $entity->GetAttributes();
         /* get all attributes from entity */
        $i = '0';
        while ($row = mssql_fetch_row($status))
        {
             /* for every attribute */
            foreach ($attributes as $attr)
            {
                if ($attr->GetName() == $row[0]) /* update the value */
                    $attr->SetValue($row[1]);
            }
            $i++;
            mssql_next_result($status);
        }
        return DB_OK;
    }
}

?>